home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / c_lines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  3.8 KB  |  112 lines

  1. /*
  2. **  C_LINES.C - Count lines of code in a C program.
  3. **
  4. **  NOTES:
  5. **
  6. **  1.  Skips both C and C++ style comments
  7. **  2.  Counts all lines of conditional code.
  8. **  3.  Line length limited to 256 characters.
  9. **  4.  To expand command line arguments with wildcards,
  10. **        TC/TC++/BC++  - Link in WILDARGS.OBJ.
  11. **        MSC/QC        - Link in SETARGV.OBJ.
  12. **        ZTC/C++       - Link in _MAINx.OBJ, where 'x' is the memory model.
  13. **        Watcom C/C++  - Compile & link with WILDARGV.C
  14. **
  15. **  public domain by Bob Stout
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. #define NUL '\0'
  22.  
  23. main(int argc, char *argv[])
  24. {
  25.       FILE *fp;
  26.  
  27.       while (--argc)
  28.       {
  29.             char line[256];
  30.             int lines = 0;
  31.  
  32.             if (NULL == (fp = fopen(*++argv, "r")))
  33.             {
  34.                   printf("Can't open %s - skipping\n", *argv);
  35.                   continue;
  36.             }
  37.             while (!feof(fp))
  38.             {
  39.                   char *p;
  40.  
  41.                   if (NULL != fgets(line, 256, fp))
  42.                   {
  43.                         /*
  44.                         ** First, strip leading spaces
  45.                         */
  46.  
  47.                         for (p = line; ' ' == *p; ++p)
  48.                               ;
  49. TEST:                   switch (*p)
  50.                         {
  51.                         case NUL:         /* Don't count blank lines    */
  52.                         case '\n':
  53.                               continue;
  54.                         default:
  55.                               /* Ignore C++ comments                    */
  56.  
  57.                               if (0 == strncmp("//", p, 2))
  58.                                     continue;
  59.  
  60.                               /* Look for C comments                    */
  61.  
  62.                               if (0 == strncmp("/*", p, 2))
  63.                               {
  64. SKIP:                               while (NULL == (p = strstr(line, "*/")))
  65.                                     {
  66.                                           if (feof(fp))
  67.                                           {
  68.                                                 printf("%-12s: * Unterminated"
  69.                                                       " comment error *\n",
  70.                                                       strupr(*argv));
  71.                                                 break;
  72.                                           }
  73.                                           fgets(line, 256, fp);
  74.                                     }
  75.                                     p += 2;
  76.                                     goto TEST;
  77.                               }
  78.  
  79.                         }
  80.                         ++lines;
  81.  
  82.                         /*
  83.                         ** Look for embedded C comment starts,
  84.                         ** ignore them if within quotes.
  85.                         */
  86.  
  87.                         if (NULL != strstr(p, "/*"))
  88.                         {
  89.                               int quotes;
  90.  
  91.                               *p = NUL;
  92.                               for (quotes = 0;
  93.                                     NULL != (p = strrchr(line, '\"'));
  94.                                     ++quotes)
  95.                               {
  96.                                     if (p != line &&
  97.                                           NULL != strchr("'\\", *(p - 1)))
  98.                                     {
  99.                                           --quotes;
  100.                                     }
  101.                                     *p = NUL;
  102.                               }
  103.                               if (0 != (quotes & 1))
  104.                                     goto SKIP;
  105.                         }
  106.                   }
  107.             }
  108.             fclose(fp);
  109.             printf("%-12s: %3d Lines\n", strupr(*argv), lines);
  110.       }
  111. }
  112.